package ca.eloas.gpsclock.widget;

import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * User: jpbelang
 * Date: Sep 17, 2010
 * Time: 5:15:51 PM
 */
public class DigitalClock extends TextView {

    private Handler handler = new Handler();
    private SimpleDateFormat dateFormat =  new SimpleDateFormat("hh:mm");

    public DigitalClock(Context context) {
        super(context);
    }


    public DigitalClock(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public DigitalClock(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }


    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        setupHandler();
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
    }

    private void setupHandler() {

        handler.postDelayed(new Runnable() {

            public void run() {

                updateTime();
                handler.postDelayed(this, 60 * 1000);
            }
        }, (60 * 1000) - System.currentTimeMillis()% 1000);

        updateTime();
    }
    
    private void updateTime() {

        this.setText(dateFormat.format(new Date()));
    }
}
